﻿/*
创建人：longb
日期：2022-06-14
描述：缺件统计js
*/
var page = {
    pagetotal: 0,
    pageSize: 20,
    pageNumber: 1,
    editIndex: null, //easyUI单元格编辑
    rootPath: getRootPath(), //网站根路径
    ajaxUrl: getRootPath() + "/Ashx/ZsHandler/ZSFullSetStatisticsAnalysisHandler.ashx",
    //初始化页面
    init: function () {
        page.bind();
        customizedEasyuiPagination('pp'); //定制化分页
    },
    //页面事件绑定
    bind: function () {
        $('#OrderType').combobox({
            data: [{ id: 1, text: '正常' }, { id: 0, text: '临时'}],
            valueField: 'id',
            textField: 'text'
        });
        $('#BigSmall').combobox({
            data: [{ id: 0, text: '否' }, { id: 1, text: '是'}],
            valueField: 'id',
            textField: 'text'
        });
        $('#OrderType').combobox('setValue', 1);
        $('#BigSmall').combobox('setValue', 1);
        //任务单信息
        $("#gdContainerInfo").datagrid({
            checkOnSelect: false,
            selectOnCheck: false,
            singleSelect: true,
            onLoadSuccess: function (data) {
                $('#pp').pagination('refresh', {	// 改变选项，并刷新分页栏信息
                    total: page.pagetotal,
                    pageSize: page.pageSize,
                    pageNumber: page.pageNumber
                });
                customizedEasyuiPagination('pp'); //定制化分页
            },
            onClickCell: function (index, field,value) {
                if(field!="OP"){
                    $('#gdContainerInfo').datagrid('selectRow',index);
                    page.GetSpec();
                    page.GetBigSmallData('');
                }
            },
            columns: [[
                 //{ field: 'ck', title: '选择', checkbox: true },
                 { field: 'ATTRIBUTEVALUE', title: '发动机号', width: 120 },
                 { field: 'MFGORDERNAME', title: '订单号', width: 150 },
		         { field: 'TOGETHERFLAG', title: '任务单号', width: 180 },
		         { field: 'WORKFLOWTYPE', title: '任务类型', width: 130 }, //--
                 {field: 'PRODUCTNAME', title: '件号', width: 120 },
                 { field: 'DESCRIPTION', title: '件号名称', width: 90 },
                 { field: 'WFDESC', title: '工艺名称', width: 120 },
                 { field: 'PLANNEDSTARTDATE', title: '计划开始时间', width: 130, formatter: function (value, row, index) {
                     if (value != null)
                         return dayjs(value).format("YYYY-MM-DD");
                 }
                 },
                 { field: 'PLANNEDFINISHDATE', title: '计划完成时间', width: 130, formatter: function (value, row, index) {
                     if (value != null)
                         return dayjs(value).format("YYYY-MM-DD");
                 }
                 },
            //                 { field: 'TASKBILLSTATUS', title: '任务单状态', width: 100 },
                 { field: 'WORKFLOWSTEPNAME', title: '阶段', width: 100 },
                 { field: 'QTL', title: '齐套率', width: 120, align: 'center', formatter: function (value, row, index) {
                     //if (row.STEPCOUNT == null) { return "0%"; } else { return parseInt(row.QT / row.STEPCOUNT * 100) + '%'; } 
                     return parseInt(value * 100) + '%';
                 }
                 },
                { field: 'OP', title: '操作', width: 100,align: 'center', formatter: function (value, row, index) {
                    return '<a href="javascript:void(0)" id="btnReset" class="easyui-linkbutton l-btn" style="text-decoration:none;width:70px" onclick="page.CreateTaskOrder('+index+')">到料确认</a>';
                } 
                }
	           ]]
        });
        //工序信息
        $('#StepTable').datagrid({
            fitColumns: true,
            singleSelect: true,
            columns: [[
		            { field: 'SPECNO', title: '工序号', width: 50, align: 'center' },
		            { field: 'OPDESC', title: '工序名称', width: 150 }
                ]],
            onSelect: function (rowIndex, rowData) {
                //console.info(rowData);
                $('#BigSmallTable').datagrid('loadData', [])
                page.GetBigSmallData(rowData.SPECID);
            }
        });
        //大小件信息
        $('#BigSmallTable').datagrid({
            //fitColumns: true,
            checkOnSelect: false,
            columns: [[
		            { field: 'PRODUCTNAME', title: '件号', width: 100 },
		            { field: 'DESCRIPTION', title: '件号名称', width: 100 },
                    { field: 'PARTTYPE', title: '零件类型', width: 100, align: 'center', formatter: function (value, row, index) { if (value == "0") { return "大件"; } else { return "小件"; } } },
		            { field: 'FIRSTASSEMBLEQTY', title: '单台数量', width: 100, align: 'center' },
                    { field: 'CONFIRMQTY', title: '已报工数量', width: 100, align: 'center' },
                    { field: 'ISSTOTAL', title: '已报工数量', width: 100, align: 'center', hidden: true },
                    { field: 'NOTOTALCOLLECTQTY', title: '未报工数量', width: 100, align: 'center', formatter: function (value, row, index) { var qty = row.FIRSTASSEMBLEQTY - row.CONFIRMQTY; if (qty > 0) { return qty; } else { return 0; } } },
                     { field: 'QTL', title: '齐套率', width: 100, align: 'center', formatter: function (value, row, index) { return parseInt(row.CONFIRMQTY / row.FIRSTASSEMBLEQTY * 100) + '%'; } },
                ]]
        });
        //分页事件
        $('#pp').pagination({
            onSelectPage: function (pageNumber, pageSize) {
                customizedEasyuiPagination('pp'); //定制化分页
                page.pageSize = pageSize; page.pageNumber = pageNumber;
                page.GetTaskOrderData();
            }
        });
        //绑定分页go按钮
        $('#pageGo').on("click", function () {
            var pageNumber = $(this).parents('tr').find('.pagination-num').val();
            page.pageNumber = pageNumber;
            page.GetTaskOrderData();
        });
        //清除按钮
        $('#btnReset').on("click", function () {
            page.reset(true, true);
            page.pageNumber = 1; page.pagetotal = 0
            $('#pp').pagination('refresh', {
                total: page.pagetotal,
                pageSize: page.pageSize,
                pageNumber: page.pageNumber
            });
        });


    },
    //查询
    searchData: function () {
        page.reset(false, true);
        page.pageNumber = 1;
        $('#BigSmallTable').datagrid('loadData', []);
        page.GetTaskOrderData();
    },
    //查询任务单信息
    GetTaskOrderData: function () {
        $('#StepTable').datagrid('loadData', [])
        $('#BigSmallTable').datagrid('loadData', [])
        var frdata = $('#searchForm').serializeArray();
        var data = {};
        $.each(frdata, function () {
            data[this.name] = this.value
        });
        var param = {
            action: "GetIssueMaterialLackQueryData",
            pageSize: page.pageSize,
            pageNumber: page.pageNumber,
            data: JSON.stringify([data])
        };
        page.post(null,param, function (res) {
            page.pagetotal = res.RowCount;
            $('#gdContainerInfo').datagrid('loadData', res.DBTable);
        });
    },
    //查询工序信息
    GetSpec: function () {
        $('#BigSmallTable').datagrid('loadData', []);
        var row = $('#gdContainerInfo').datagrid('getSelected');
        var param = {
            action: "GetSpec",
            workflowid: row.WORKFLOWID,
            ContainerId: row.CONTAINERID,
            ContainerNo: row.TOGETHERFLAG,
            mfgorderid:row.MFGORDERID
        };
        page.post(null,param, function (res) {
            $('#StepTable').datagrid('loadData', res.DBTable)
        });
    },
    //大小件信息
    GetBigSmallData: function (workflowstepid) {
        var row = $('#gdContainerInfo').datagrid('getSelected');
        var param = {
            action: "GetBigSmallData",
            workflowstepid: workflowstepid,
            ContainerNo: row.TOGETHERFLAG,
            ContainerId: row.CONTAINERID,
            mfgorderid:row.MFGORDERID
        };
        page.post(null,param, function (res) {
            var data = res.DBTable;
            data = data.concat(res.ReturnData);
            $('#BigSmallTable').datagrid('loadData', data)
        });

    },
    //查询重置
    searchReset: function () {
        $('#searchForm').form('clear');
        $('#OrderType').combobox('setValue', 1);
    },
    //到料确认--生成任务单
    CreateTaskOrder:function(index){
        clearMessage();
        var row=$('#gdContainerInfo').datagrid('getRows')[index]
        var param = {
            action: "CreateTaskOrder",
            HasBom:"1",
            data: JSON.stringify([row]),
        };
        page.post(page.rootPath+"/Ashx/ZsHandler/ZSTaskOrderManagementHandler.ashx",param, function (res) {
            if(res.Result){page.GetTaskOrderData();}
           displayMessage(res.Message, res.Result);
        });
    },
    //批量报工
    BatchIssueColloge: function (index) {
        var rows = $('#BigSmallTable').datagrid('getChecked');
        if (index != null) {
            rows = [$('#BigSmallTable').datagrid('getRows')[index]];
        }
        if (rows.length == 0) {
            displayMessage("请先勾选大小件信息，再进行报工");
            return;
        }
        //        if (rows.length == 1) {
        //            if (rows[0].EDITORQTY == 0) {
        //                displayMessage("报工数量必须大于0");
        //                return;
        //             }
        //         }
        var data = [];
        $.each(rows, function (index, item) {
            if (item.EDITORQTY == 0) {//如果没有点单元格编辑，默认值是0 ，且计算出【未报工数量】<=【确认数量】压进报工数组
                if ((Number(item.CONFIRMQTY) - Number(item.TOTALCOLLECTQTY)) <= Number(item.CONFIRMQTY)) {
                    item.EDITORQTY = (Number(item.CONFIRMQTY) - Number(item.TOTALCOLLECTQTY));
                    data.push(item);
                }
            } else {//【上报数量】+【已报工数量】<=确认数量】压进报工数组
                if ((Number(item.EDITORQTY) + Number(item.TOTALCOLLECTQTY)) <= Number(item.CONFIRMQTY)) {
                    data.push(item);
                }
            }
        });
        if (data.length == 0 || rows.length != data.length) { displayMessage("报工数量大于实际收料数量，不能报工", false); return; }
        //console.info(rows);
        var param = {
            action: "SaveIssueERPMaterialConfirmation",
            type: page.pageType,
            data: JSON.stringify(data)
        };

        page.post(null,param, function (res) {
            if (res.Result) {
                page.GetBigSmallData($('#StepTable').datagrid("getSelected").WORKFLOWSTEPID);
            }
            displayMessage(res.Message, res.Result);
        });

    },
    //清除方法，重要！！！
    reset: function (iscondition, isresult) {
        clearMessage();
    },
    //post数据
    post: function (url,param, callback) {
        $.ajax({
            type: "Post",
            url: url==null?page.ajaxUrl:url,
            data: param,
            cashe: false,
            async: true,
            dataType: "json",
            beforeSend: function () {
                $.messager.progress({ text: '', msg: '处理中...' });
            },
            success: function (data) {
                $.messager.progress('close');
                callback(data);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                $.messager.progress('close');
                displayMessage(XMLHttpRequest.responseText, false);
            }
        });
    }
}
$(function () {
    page.init();
});